//Create variables that code randomization

global M_Baseline 	"17273618505 17273618470 17273802761 17273618474 17273804544 17273618471 17273804424 17273618476" //0
global M_T1			"17273618475 17273618112 17273618757 17273618479 17273618116 17273802926 17273803981 17273802890" //1
global M_T2 		"17273618980 17273618463 17273618462 17273616065 17273618581 17273618588 17273618467 17273546974" //2
global M_T3 		"17273618586 17273618464 17273618623 17273618468 17273798466 17273618969 17273546999 17273546972" //3
global M_T4 		"17273546997 17273803906 17273546996 17273546981 17273546994 17273804166 17273618450 17273618570" //4

global Pay_Baseline "17273618973 17273546967 17273618574 17273618974 17273618959 17273546989 17273546988 17273546965" //5
global Pay_T1		"17273546986 17273546962 17273546984 17273798462 17273546993 17273546956 17273546990 17273618683" //6
global Pay_T2 		"17273619530 17273546954 17273619495 17273546978 17273618680 17273618324 17273618688 17273618706" //7
global Pay_T3 		"17273618705 17273803929 17273618704 17273802955 17273546979 17273619494 17273546977 17273802954" //8
global Pay_T4 		"17273546976 17273618466 17273546973 17273546998 17273546982 17273546995 17273546980 17273618672" //9

global Ex_Baseline 	"17273616092 17273618153 17273803016 17273802685 17273618152 17273618797 17273618559 17273804220" //10
global Ex_T1		"17273618679 17273618939 17273546968 17273618971 17273546966 17273546987 17273546964 17273619071" //11
global Ex_T2 		"17273546963 17273546985 17273804509 17273546961 17273546971 17273616084 17273546970 17273616087" //12
global Ex_T3 		"17273618781 17273618143 17273618148 17273618664 17273618823 17273618666 17273618667 17273616089" //13
global Ex_T4 		"17273618149 17273798464 17273798343 17273618803 17273798461 17273546983 17273798460 17273546959" //14

global FT_Baseline 	"17273546957 17273546992 17273546955 17273619496 17273546953 17273616074 17273546951 17273616072" //15
global FT_T1		"17273616073 17273546952 17273803036 17273616075 17273804403 17273616070 17273618891 17273804404" //16
global FT_T2 		"17273616071 17273546950 17273618896 17273618137 17273618136 17273618772 17273618530 17273798459" //17
global FT_T3 		"17273616997 17273618138 17273618759 17273618912 17273618879 17273546948 17273546947 17273802867" //18
global FT_T4 		"17273616062 17273618461 17273804013 17273618247 17273804370 17273618883 17273616068 17273618248" //19

global Variations "M Pay Ex FT"	
//global Treatments "Baseline T1 T2 T3 T4"	

gen Variation=.
// DH add
destring tonumber, replace 
format tonumber %20.0f
local i = 0
foreach variation in $Variations{
foreach num in $`variation'_Baseline {
	replace Variation=`i' if tonumber==`num'
	}
foreach num in $`variation'_T1 {
	replace Variation=`i' if tonumber==`num'
	}
foreach num in $`variation'_T2 {
	replace Variation=`i' if tonumber==`num'
	}
foreach num in $`variation'_T3 {
	replace Variation=`i' if tonumber==`num'
	}
foreach num in $`variation'_T4 {
	replace Variation=`i' if tonumber==`num'
	}	
local i = `i' + 1	
}

label define Variationl ///
	0 "Main" ///
	1 "Payments" ///
	2 "Both Experts"	///
	3 "Both Full Time" 
label values Variation Variationl


label define Treatl ///
	0 "Baseline" ///
	1 "Male High Availability" ///
	2 "Male Low Availability"	///
	3 "Female High Availability" ///
	4 "Female Low Availability" 

gen Treatment=.	
foreach variation in $Variations{
foreach num in $`variation'_Baseline {
	replace Treatment=0 if tonumber==`num'
	}
foreach num in $`variation'_T1 {
	replace Treatment=1 if tonumber==`num'
	}	
foreach num in $`variation'_T2 {
	replace Treatment=2 if tonumber==`num'
	}	
foreach num in $`variation'_T3 {
	replace Treatment=3 if tonumber==`num'
	}	
foreach num in $`variation'_T4 {
	replace Treatment=4 if tonumber==`num'
	}		
label values Treatment Treatl
}

//Create variables that code gender of phone number
gen FemaleNum=.
global FemaleNum 	"17273618505 17273802761 17273804544 17273804424 17273618475 17273618757 17273618116 17273803981 17273618980 17273618462 17273618581 17273618467 17273618586 17273618623 17273798466 17273546999 17273546997 17273546996 17273546994 17273618450 17273618973 17273618574 17273618959 17273546988 17273546986 17273546984 17273546993 17273546990 17273619530 17273619495 17273618680 17273618688 17273618705 17273618704 17273546979 17273546977 17273546976 17273546973 17273546982 17273546980 17273616092 17273803016 17273618152 17273618559 17273618679 17273546968 17273546966 17273546964 17273546963 17273804509 17273546971 17273546970 17273618781 17273618148 17273618823 17273618667 17273618149 17273798343 17273798461 17273798460 17273546957 17273546955 17273546953 17273546951 17273616073 17273803036 17273804403 17273618891 17273616071 17273618896 17273618136 17273618530 17273616997 17273618759 17273618879 17273546947 17273616062 17273804013 17273804370 17273616068"
global MaleNum 		"17273618470 17273618474 17273618471 17273618476 17273618112 17273618479 17273802926 17273802890 17273618463 17273616065 17273618588 17273546974 17273618464 17273618468 17273618969 17273546972 17273803906 17273546981 17273804166 17273618570 17273546967 17273618974 17273546989 17273546965 17273546962 17273798462 17273546956 17273618683 17273546954 17273546978 17273618324 17273618706 17273803929 17273802955 17273619494 17273802954 17273618466 17273546998 17273546995 17273618672 17273618153 17273802685 17273618797 17273804220 17273618939 17273618971 17273546987 17273619071 17273546985 17273546961 17273616084 17273616087 17273618143 17273618664 17273618666 17273616089 17273798464 17273618803 17273546983 17273546959 17273546992 17273619496 17273616074 17273616072 17273546952 17273616075 17273616070 17273804404 17273546950 17273618137 17273618772 17273798459 17273618138 17273618912 17273546948 17273802867 17273618461 17273618247 17273618883 17273618248"

foreach num in $FemaleNum {
	replace FemaleNum= 1 if tonumber==`num'
	}

foreach num in $MaleNum {
	replace FemaleNum= 0 if tonumber==`num'
	}

label define FemaleNuml ///
	0 "Male Number (Dad)" ///
	1 "Female Number (Mom)" 
label values FemaleNum FemaleNuml	
	
//Create variables that code gender of decision-maker
gen FemalePrincipal=.
global FemalePrincipal 	"17273618505 17273618470 17273802761 17273618474 17273618475 17273618112 17273618757 17273618479 17273618980 17273618463 17273618462 17273616065 17273618586 17273618464 17273618623 17273618468 17273546997 17273803906 17273546996 17273546981 17273618973 17273546967 17273618574 17273618974 17273546986 17273546962 17273546984 17273798462 17273619530 17273546954 17273619495 17273546978 17273618705 17273803929 17273618704 17273802955 17273546976 17273618466 17273546973 17273546998 17273616092 17273618153 17273803016 17273802685 17273618679 17273618939 17273546968 17273618971 17273546963 17273546985 17273804509 17273546961 17273618781 17273618143 17273618148 17273618664 17273618149 17273798464 17273798343 17273618803 17273546957 17273546992 17273546955 17273619496 17273616073 17273546952 17273803036 17273616075 17273616071 17273546950 17273618896 17273618137 17273616997 17273618138 17273618759 17273618912 17273616062 17273618461 17273804013 17273618247"
global MalePrincipal 	"17273804544 17273618471 17273804424 17273618476 17273618116 17273802926 17273803981 17273802890 17273618581 17273618588 17273618467 17273546974 17273798466 17273618969 17273546999 17273546972 17273546994 17273804166 17273618450 17273618570 17273618959 17273546989 17273546988 17273546965 17273546993 17273546956 17273546990 17273618683 17273618680 17273618324 17273618688 17273618706 17273546979 17273619494 17273546977 17273802954 17273546982 17273546995 17273546980 17273618672 17273618152 17273618797 17273618559 17273804220 17273546966 17273546987 17273546964 17273619071 17273546971 17273616084 17273546970 17273616087 17273618823 17273618666 17273618667 17273616089 17273798461 17273546983 17273798460 17273546959 17273546953 17273616074 17273546951 17273616072 17273804403 17273616070 17273618891 17273804404 17273618136 17273618772 17273618530 17273798459 17273618879 17273546948 17273546947 17273802867 17273804370 17273618883 17273616068 17273618248"

foreach num in $FemalePrincipal {
	replace FemalePrincipal= 1 if tonumber==`num'
	}

foreach num in $MalePrincipal {
	replace FemalePrincipal= 0 if tonumber==`num'
	}

label define FemalePrincipall ///
	0 "Male Principal" ///
	1 "Female Principal" 
label values FemalePrincipal FemalePrincipall		
	
//Create variables for order effects
gen MomListedFirst=.
global MomListedFirst 	"17273618505 17273618470 17273804544 17273618471 17273618475 17273618112 17273618116 17273802926 17273618980 17273618463 17273618581 17273618588 17273618586 17273618464 17273798466 17273618969 17273546997 17273803906 17273546994 17273804166 17273618973 17273546967 17273618959 17273546989 17273546986 17273546962 17273546993 17273546956 17273619530 17273546954 17273618680 17273618324 17273618705 17273803929 17273546979 17273619494 17273546976 17273618466 17273546982 17273546995 17273616092 17273618153 17273618152 17273618797 17273618679 17273618939 17273546966 17273546987 17273546963 17273546985 17273546971 17273616084 17273618781 17273618143 17273618823 17273618666 17273618149 17273798464 17273798461 17273546983 17273546957 17273546992 17273546953 17273616074 17273616073 17273546952 17273804403 17273616070 17273616071 17273546950 17273618136 17273618772 17273616997 17273618138 17273618879 17273546948 17273616062 17273618461 17273804370 17273618883"
global DadListedFirst 	"17273802761 17273618474 17273804424 17273618476 17273618757 17273618479 17273803981 17273802890 17273618462 17273616065 17273618467 17273546974 17273618623 17273618468 17273546999 17273546972 17273546996 17273546981 17273618450 17273618570 17273618574 17273618974 17273546988 17273546965 17273546984 17273798462 17273546990 17273618683 17273619495 17273546978 17273618688 17273618706 17273618704 17273802955 17273546977 17273802954 17273546973 17273546998 17273546980 17273618672 17273803016 17273802685 17273618559 17273804220 17273546968 17273618971 17273546964 17273619071 17273804509 17273546961 17273546970 17273616087 17273618148 17273618664 17273618667 17273616089 17273798343 17273618803 17273798460 17273546959 17273546955 17273619496 17273546951 17273616072 17273803036 17273616075 17273618891 17273804404 17273618896 17273618137 17273618530 17273798459 17273618759 17273618912 17273546947 17273802867 17273804013 17273618247 17273616068 17273618248"

foreach num in $MomListedFirst {
	replace MomListedFirst= 1 if tonumber==`num'
	}

foreach num in $DadListedFirst {
	replace MomListedFirst= 0 if tonumber==`num'
	}

label define MomListedFirstl ///
	0 "Dad Listed 1st" ///
	1 "Mom Listed 1st"
label values MomListedFirst MomListedFirstl			
	
//Create Outcome variable (ideal is 1 per email)
//Dealing with multiple calls from same person
sort fromnumber
gen ContactName6=substr(fromnumber,2,6)
gen ContactNameFormat=substr(fromnumber,2,10)
destring ContactNameFormat, force replace
	//destring contactname, ignore("-, (, ), , Restricted") gen(ContactNameFormat)
format ContactNameFormat %20.0f
	//destring ContactName6, ignore("-, (, ), , Restricted") gen(ContactNameFormat6)
destring ContactName6, force gen(ContactNameFormat6)
format ContactNameFormat6 %20.0f
unique ContactNameFormat //
unique ContactNameFormat6 //
format tonumber %20.0f

//answertime 

gen date=substr(answertime, 1,5)+substr(answertime, 7,2)+substr(answertime, 10,2)+substr(answertime, 13,2)+substr(answertime, 16,2)+substr(answertime, 19,2)
generate double numtime = clock(date, "YMDhms")	
format numtime %tc


egen phoneID=group(fromnumber) // DH change
	//Create an identifier for "Restricted" by time
	egen phoneID_temp=group(fromnumber numtime) if fromnumber=="D0000251251"
	replace phoneID=phoneID+phoneID_temp-1 if fromnumber=="D0000251251"
	tostring phoneID_temp, replace
	tostring ContactNameFormat, replace
	tostring ContactNameFormat6, replace
	replace ContactNameFormat="1112220000"+phoneID_temp if fromnumber=="D0000251251"
	replace ContactNameFormat6="111220"+phoneID_temp if fromnumber=="D0000251251"
	drop phoneID_temp
	replace phoneID = phoneID + 100000 if fromnumber=="D0000251251"

egen CallfireID=group(phoneID)

gen PhoneNumPair=.
	replace PhoneNumPair=1 if tonumber==17273618505|tonumber==17273618470
	replace PhoneNumPair=2 if tonumber==17273618474|tonumber==17273802761
	replace PhoneNumPair=3 if tonumber==17273618475|tonumber==17273618112
	replace PhoneNumPair=4 if tonumber==17273618479|tonumber==17273618757
	replace PhoneNumPair=5 if tonumber==17273618980|tonumber==17273618463
	replace PhoneNumPair=6 if tonumber==17273616065|tonumber==17273618462
	replace PhoneNumPair=7 if tonumber==17273618586|tonumber==17273618464
	replace PhoneNumPair=8 if tonumber==17273618468|tonumber==17273618623
	replace PhoneNumPair=9 if tonumber==17273546997|tonumber==17273803906
	replace PhoneNumPair=10 if tonumber==17273546981|tonumber==17273546996
	replace PhoneNumPair=11 if tonumber==17273618973|tonumber==17273546967
	replace PhoneNumPair=12 if tonumber==17273618974|tonumber==17273618574
	replace PhoneNumPair=13 if tonumber==17273546986|tonumber==17273546962
	replace PhoneNumPair=14 if tonumber==17273798462|tonumber==17273546984
	replace PhoneNumPair=15 if tonumber==17273619530|tonumber==17273546954
	replace PhoneNumPair=16 if tonumber==17273546978|tonumber==17273619495
	replace PhoneNumPair=17 if tonumber==17273618705|tonumber==17273803929
	replace PhoneNumPair=18 if tonumber==17273802955|tonumber==17273618704
	replace PhoneNumPair=19 if tonumber==17273546976|tonumber==17273618466
	replace PhoneNumPair=20 if tonumber==17273546998|tonumber==17273546973
	replace PhoneNumPair=21 if tonumber==17273616092|tonumber==17273618153
	replace PhoneNumPair=22 if tonumber==17273802685|tonumber==17273803016
	replace PhoneNumPair=23 if tonumber==17273618679|tonumber==17273618939
	replace PhoneNumPair=24 if tonumber==17273618971|tonumber==17273546968
	replace PhoneNumPair=25 if tonumber==17273546963|tonumber==17273546985
	replace PhoneNumPair=26 if tonumber==17273546961|tonumber==17273804509
	replace PhoneNumPair=27 if tonumber==17273618781|tonumber==17273618143
	replace PhoneNumPair=28 if tonumber==17273618664|tonumber==17273618148
	replace PhoneNumPair=29 if tonumber==17273618149|tonumber==17273798464
	replace PhoneNumPair=30 if tonumber==17273618803|tonumber==17273798343
	replace PhoneNumPair=31 if tonumber==17273546957|tonumber==17273546992
	replace PhoneNumPair=32 if tonumber==17273619496|tonumber==17273546955
	replace PhoneNumPair=33 if tonumber==17273616073|tonumber==17273546952
	replace PhoneNumPair=34 if tonumber==17273616075|tonumber==17273803036
	replace PhoneNumPair=35 if tonumber==17273616071|tonumber==17273546950
	replace PhoneNumPair=36 if tonumber==17273618137|tonumber==17273618896
	replace PhoneNumPair=37 if tonumber==17273616997|tonumber==17273618138
	replace PhoneNumPair=38 if tonumber==17273618912|tonumber==17273618759
	replace PhoneNumPair=39 if tonumber==17273616062|tonumber==17273618461
	replace PhoneNumPair=40 if tonumber==17273618247|tonumber==17273804013
	replace PhoneNumPair=41 if tonumber==17273804544|tonumber==17273618471
	replace PhoneNumPair=42 if tonumber==17273618476|tonumber==17273804424
	replace PhoneNumPair=43 if tonumber==17273618116|tonumber==17273802926
	replace PhoneNumPair=44 if tonumber==17273802890|tonumber==17273803981
	replace PhoneNumPair=45 if tonumber==17273618581|tonumber==17273618588
	replace PhoneNumPair=46 if tonumber==17273546974|tonumber==17273618467
	replace PhoneNumPair=47 if tonumber==17273798466|tonumber==17273618969
	replace PhoneNumPair=48 if tonumber==17273546972|tonumber==17273546999
	replace PhoneNumPair=49 if tonumber==17273546994|tonumber==17273804166
	replace PhoneNumPair=50 if tonumber==17273618570|tonumber==17273618450
	replace PhoneNumPair=51 if tonumber==17273618959|tonumber==17273546989
	replace PhoneNumPair=52 if tonumber==17273546965|tonumber==17273546988
	replace PhoneNumPair=53 if tonumber==17273546993|tonumber==17273546956
	replace PhoneNumPair=54 if tonumber==17273618683|tonumber==17273546990
	replace PhoneNumPair=55 if tonumber==17273618680|tonumber==17273618324
	replace PhoneNumPair=56 if tonumber==17273618706|tonumber==17273618688
	replace PhoneNumPair=57 if tonumber==17273546979|tonumber==17273619494
	replace PhoneNumPair=58 if tonumber==17273802954|tonumber==17273546977
	replace PhoneNumPair=59 if tonumber==17273546982|tonumber==17273546995
	replace PhoneNumPair=60 if tonumber==17273618672|tonumber==17273546980
	replace PhoneNumPair=61 if tonumber==17273618152|tonumber==17273618797
	replace PhoneNumPair=62 if tonumber==17273804220|tonumber==17273618559
	replace PhoneNumPair=63 if tonumber==17273546966|tonumber==17273546987
	replace PhoneNumPair=64 if tonumber==17273619071|tonumber==17273546964
	replace PhoneNumPair=65 if tonumber==17273546971|tonumber==17273616084
	replace PhoneNumPair=66 if tonumber==17273616087|tonumber==17273546970
	replace PhoneNumPair=67 if tonumber==17273618823|tonumber==17273618666
	replace PhoneNumPair=68 if tonumber==17273616089|tonumber==17273618667
	replace PhoneNumPair=69 if tonumber==17273798461|tonumber==17273546983
	replace PhoneNumPair=70 if tonumber==17273546959|tonumber==17273798460
	replace PhoneNumPair=71 if tonumber==17273546953|tonumber==17273616074
	replace PhoneNumPair=72 if tonumber==17273616072|tonumber==17273546951
	replace PhoneNumPair=73 if tonumber==17273804403|tonumber==17273616070
	replace PhoneNumPair=74 if tonumber==17273804404|tonumber==17273618891
	replace PhoneNumPair=75 if tonumber==17273618136|tonumber==17273618772
	replace PhoneNumPair=76 if tonumber==17273798459|tonumber==17273618530
	replace PhoneNumPair=77 if tonumber==17273618879|tonumber==17273546948
	replace PhoneNumPair=78 if tonumber==17273802867|tonumber==17273546947
	replace PhoneNumPair=79 if tonumber==17273804370|tonumber==17273618883
	replace PhoneNumPair=80 if tonumber==17273618248|tonumber==17273616068
	
gen DaysSinceStartingEmails=.
	replace DaysSinceStartingEmails= dofc(numtime) - mdy(4,23,2021) if Wave==1
	replace DaysSinceStartingEmails= dofc(numtime) - mdy(6,14,2021) if Wave==2
	replace DaysSinceStartingEmails= dofc(numtime) - mdy(5,24,2022) if Wave==4
	replace DaysSinceStartingEmails= dofc(numtime) - mdy(7,11,2022) if Wave==5	

//keep if DaysSinceStartingEmails <=15 //CHANGE TO 14 LATER?
keep if DaysSinceStartingEmails >-1

bysort phoneID PhoneNumPair: gen NumCalls=_N //This is the number of times a specific phone number called either number in a mother father pair
sort phoneID PhoneNumPair numtime
	by phoneID PhoneNumPair: gen CallOrder=_n //This orders the phone calls within a phone number and mother father pairing
		
sort ContactNameFormat CallOrder	
